<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//社区分类管理
class Bbs_article_cate_model extends CI_Model{

        function __construct()
        {
                parent::__construct();
                $this->table = 'bbs_article_category';//文章分类
        }
        
        public function get_Category_List($where)
        {
                $this->db->select('*');
                $this->db->from($this->table);
                $this->db->where($where);
                $this->db->order_by('sort desc');
                $query = $this->db->get();
                return $query->result_array();
        }
        
         public function getCategoryList($pid)
        {
                $this->db->select('*');
                $this->db->from($this->table);
                $this->db->where('pid',$pid);
                $this->db->order_by('sort');
                //$this->db->limit($limit);
                $query = $this->db->get();
                return $query->result_array();
        }
        public function get_content_list(){
                $this->db->select('*');
                $this->db->from($this->table);
                $this->db->order_by('sort,id asc');
                //$this->db->limit($limit);
                $query = $this->db->get();
                return $query->result_array();
        }

        function get_category_data($id){
                return $query = $this->db->where('id',$id)->get($this->table)->row_array();
        }
        
        function update($data,$id)
        {
                return $this->db->where('id',$id)->update($this->table, $data);
        }
        
        function get_list()
        {
                return $query = $this->db->get($this->table)->result_array();
        }
        
        public function GetCategoryAll()
        {
                $this->db->select('*');
                $this->db->from($this->table);
                $this->db->where('is_show','0');
                $this->db->order_by('sort,id asc');
                $query = $this->db->get();
                return $query->result_array();
        }
        
        public function tree($table,$pid='0') {
                $tree = array();
                foreach($table as $row){
                        if($row['pid']==$pid){
                                $tmp = $this->tree($table,$row['id']);
                                if($tmp){
                                        $row['children']=$tmp;
                                }else{
                                        $row['leaf'] = true;
                                }
                                $tree[]=$row;                
                        }
                }
                return $tree;        
        }
        
        public function insert($data){
            $this->db->insert($this->table, $data);
            return $this->db->affected_rows();
        }
        function category($pid){
        $this->db->select('id,name');
        $this->db->from($this->table);
        $this->db->where('pid',$pid);
        $this->db->where('is_show','0');
        $this->db->order_by('sort','asc');
        return $this->db->get()->result_array();
    }
}